//
//  ZJEmotionPictureCell.swift
//  Webo
//
//  Created by 曾静 on 16/7/5.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit

class ZJEmotionPictureCell: UICollectionViewCell {
    
    var indexPath:NSIndexPath?{
        
        
        didSet{
            
            desLabel.text = "第\(indexPath?.section ?? 0)组,第\(indexPath?.item ?? 0)个item"
            
        }
    }
    
    var emotionButtons:[UIButton] = [UIButton]()
    
    var emotions:[ZJEmotion]?{
        
        didSet{
            
            //将所有的按钮隐藏掉,因为以后的cell要重用
            for button in emotionButtons{
                
                button.hidden = true
            }
            
            
            //如果有值的话就为button的image或者是title赋值
            
            for (i,emotion) in emotions!.enumerate(){
                
                let button = emotionButtons[i]
                
                button.hidden = false
                
                
                if emotion.isEmoji {
                    
//                    button.backgroundColor = UIColor.whiteColor()
                    
                    let tit = ((emotion.code ?? "") as NSString).emoji()
                    
                    button.setTitle(tit, forState: UIControlState.Normal)
                    // 考虑到cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                    
//                    print(emotion.code)

                    
                    
                }else{
                    //默认不是emoji表情
                    
                    //取出图片的具体路径
                    
                    //仅仅是图片的名称,bundle只能获取到Emoticons.bundle
                    let png = emotion.png ?? ""
                
                    let path = emotion.path ?? ""
                   //可选类型不能直接参与运算
                    let name = path + png
                    
                    
                    let image = UIImage(named:name , inBundle: ZJEmotionTool.shareEmotionTools.emotionBundle, compatibleWithTraitCollection: nil)
                    
//                    为cell上面的button设置图片
                    
//                    print(image)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                     //由于cell重用的问题,记得将设置的文字设置为空
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
                
                
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    func setupUI(){
        
        self.backgroundColor = RandomColor()
        
        
        addButtons()
        //添加约束之前记得添加子控件
        contentView.addSubview(desLabel)
        desLabel.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(contentView)
        }
        
        //通过遍历button数组,设置frame
        let width = SCREENW / CGFloat(emotionMaxXCol)
        
        let height = self.size.height / CGFloat(emotionMaxXRow)
        
        for (i,button) in emotionButtons.enumerate(){
            
            let x = CGFloat(i % emotionMaxXCol)
            
            let y = CGFloat(i / emotionMaxXCol)
            
            button.frame = CGRect(x: x * width, y: y * height, width: width, height: height)
            
        }
        
        
        
    }
    
    //添加按钮
    func addButtons(){
        
        
        for _ in 0 ..< emotionMaxXCount{
            
            let button = UIButton()
            
            //在这里可以设置button的字体信息
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            self.emotionButtons.append(button)

            button.backgroundColor = RandomColor()
            
            contentView.addSubview(button)
        }
        
    }
    
    //懒加载label
    private lazy var desLabel:UILabel = {
      
      let desLabel =  UILabel()
        
        desLabel.font = UIFont.systemFontOfSize(35)
        
        desLabel.textColor = UIColor.redColor()
        
        desLabel.textAlignment = .Center
        
        return desLabel
    
    }()

    
}
